home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tess_c.arc / TESSDEMO.C < prev    next >
C/C++ Source or Header  |  1989-04-24  |  34KB  |  1,048 lines

  1.     /*
  2.      * TESSDEMO.C -- TesSeRact Demonstration Program
  3.      */
  4.  
  5. /******************************< TESSDEMO.C >********************************
  6. *                                                                           *
  7. *                     TesSeRact Demonstration Program                       *
  8. *                     --------------------------------                      *
  9. *                                                                           *
  10. *   Copyright (c) 1986, 1987, 1988, TesSeRact Development Team              *
  11. *                                                                           *
  12. *************************************************************************CR*/
  13.     /*
  14.      * Compiled with Turbo-C 1.5 for demonstration purposes
  15.      *   Used with small model
  16.      *   
  17.      * To compile with MSC, use /DMSC5 command-line switch
  18.      *   Note that if this is compiled with MSC 5.0, a lot of warning
  19.      *   messages are generated -- this is because of the bug in MSC5.0
  20.      *   having to do with functions prototyped with a 'void' parameter.
  21.      *   It's not worth it to fix bugs in the compiler.
  22.      * 
  23.      */
  24.  
  25. #ifndef MSC5                            /* if MSC5 not defined             */
  26. #define TC                              /*    assume Turbo C               */
  27. #endif
  28.  
  29. #include <stdio.h>
  30. #include <dos.h>
  31. #include <bios.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <io.h>
  35.  
  36. #ifdef TC
  37. #include <conio.h>
  38. #include <dir.h>
  39. #endif
  40.  
  41. #include <ctype.h>
  42. #include <errno.h>
  43.  
  44. #include "tess.h"                       /* Include file for TesSeRact     */
  45.                                         /*    structures and prototypes    */
  46.  
  47.     /*
  48.      * #define NOTSR to test routines
  49.      */
  50. /* #define NOTSR 1 */
  51.  
  52.     /*
  53.      * Function prototypes for this file
  54.      */
  55. void c_str(int row, char *str);
  56. void SaveCursor(void);
  57. void RestoreCursor(void);
  58. void NoCursor(void);
  59. void BigCursor(void);
  60. void DisplayTime(void);
  61. void AdjustTime(void);
  62. void fixrows(void);
  63. unsigned SizeOfCode(unsigned type);
  64. void InitTsrDemo(void);
  65. void do_cpyrt(void);
  66.  
  67.  
  68.     /*
  69.      * Defines to be used for 'type' parameter
  70.      *   of SizeOfCode
  71.      */
  72. #define NOHEAP  1
  73. #define ALLHEAP 2
  74. #define ALLSTACK 3
  75.  
  76. #ifdef TC
  77.     /*
  78.      * Variables specific to Turbo-C
  79.      */
  80. extern unsigned _heaplen = 128;         /* Use 128-byte heap               */
  81. extern char _video[];                   /* undocumented video structure    */
  82. extern void _VideoInt(void);            /* undocumented INT 10h call that  */
  83.                                         /*    also saves BP register to    */
  84.                                         /*    compensate for buggy BIOSes  */
  85. #endif
  86.  
  87.     /*
  88.      * Variables for Turbo-C Video Services
  89.      */
  90.  
  91. #define MAXVIDROWS  60                  /* VGA allows 50 lines             */
  92.                                         /*   But I have 60 on my screen    */
  93. #define MAXVIDCOLS  80                  /* Only dealing with 80-cols       */
  94. #define MAXVIDSIZE  (MAXVIDROWS * MAXVIDCOLS * 2)
  95.  
  96. #ifdef MSC5
  97.  
  98. #define COLORNORM   0x1e
  99. #define COLORREV    0x4f
  100.  
  101. #else
  102.  
  103. #define COLORNORM   (YELLOW + (BLUE << 4))
  104. #define COLORREV    (WHITE + (RED << 4))
  105.  
  106. #endif
  107.  
  108. #define MONONORM    0x07
  109. #define MONOREV     0x70
  110.  
  111. #ifdef TC
  112.  
  113. #define cputc putch                     /* easier to understand            */
  114.  
  115. #else
  116.  
  117. #define cprintf printf                  /* CONSOLE I/O doesn't work with   */
  118. #define cputs puts                      /*   MSC 5.x!  Some kind of problem*/
  119. #define cputc putchar                   /*   with reentrancy, I assume     */
  120.  
  121. #endif
  122.  
  123. char savescreen[MAXVIDSIZE + 10];       /* buffer to save screen image     */
  124.                                         /*   includes 10-byte fudge factor */
  125.  
  126. unsigned NormAtt,                       /* Default Normal Attribute        */
  127.          RevAtt,                        /* Default Reverse Attribute       */
  128.          curmode,                       /* Current video mode              */
  129.          pagenum,                       /* Current video page              */
  130.          curtype,                       /* Default cursor type             */
  131.          oldcur,                        /* Old Cursor shape                */
  132.          oldpos;                        /* Old Cursor position             */
  133.  
  134. unsigned maxrows = 25;                  /* Maximum rows on screen          */
  135.  
  136. unsigned char far *biosvid;             /* Pointer to video buffer         */
  137.  
  138.     /*
  139.      * Other global variables
  140.      */
  141. char buffer[10];                        /* work buffer for date/time format*/
  142.  
  143. unsigned idnum,                         /* TSR Identification Number       */
  144.          hours,                         /* Current hour of day             */
  145.          mins,                          /* Current minute of hour          */
  146.          secs,                          /* Current seconds of minute       */
  147.          ticks;                         /* Timer-tick counter              */
  148.  
  149. unsigned BackFlag = 0;                  /* Background flag to signal       */
  150.                                         /*   additional processing         */
  151.  
  152. FILE *fp = NULL;                        /* file structure for file to keep */
  153.                                         /*   open between popups           */
  154.  
  155. #define FLUSHIT(fp) fclose(fp); \
  156.                         fp = fopen("Tessdemo.dat","a+b")
  157.                         
  158.  
  159. #ifdef MSC5
  160.  
  161.         /************************************************************
  162.         * MSC 5.x compatibility routines                            *
  163.         *********************************************************CR*/
  164.  
  165. void gotoxy(unsigned x, unsigned y)     /* position cursor using BIOS      */
  166. {
  167.     union REGS regs;
  168.  
  169.     regs.h.dh = y - 1;
  170.     regs.h.dl = x - 1;
  171.  
  172.     regs.h.bh = 0;
  173.     regs.h.ah = 2;
  174.  
  175.     int86(0x10, ®s, ®s);
  176. }
  177.  
  178. void textmode(unsigned mode)            /* set video mode using BIOS       */
  179. {
  180.     union REGS regs;
  181.  
  182.     regs.h.ah = 0;
  183.     regs.h.al = mode;
  184.  
  185.     int86(0x10, ®s, ®s);
  186. }
  187.  
  188. unsigned currentattr = 0x07,            /* only needed for MSC5            */
  189.          ulc = 0, 
  190.          lrc = 0x184f;
  191.  
  192.                                         /* set local variables             */
  193. void window(int left, int top, int right, int bottom)
  194. {
  195.     ulc = ((top - 1) << 8) + (left - 1);
  196.     lrc = ((bottom - 1) << 8) + (right - 1);
  197. }
  198.  
  199. void textattr(unsigned attr)
  200. {
  201.     currentattr = attr;                 /* we don't change atts with MSC   */
  202. }
  203.  
  204. void clrscr(void)                       /* clear active window area        */
  205. {
  206.     union REGS regs;
  207.  
  208.     regs.x.ax = 0x0600;
  209.     regs.h.bh = currentattr;
  210.  
  211.     regs.x.cx = ulc;
  212.     regs.x.dx = lrc;
  213.  
  214.     int86(0x10, ®s, ®s);
  215. }
  216.  
  217.                                         /* get text from video to buffer   */
  218. int gettext(int left, int top, int right, int bottom, char *buffer)
  219. {
  220.     int i,j;
  221.     char far *video;
  222.  
  223.     for(i = top - 1; i < bottom; i++)
  224.     {
  225.         FP_SEG(video) = FP_SEG(biosvid);
  226.         FP_OFF(video) = ((i * 80) + (left - 1) ) * 2;
  227.  
  228.         for(j = left - 1; j < right; j++)
  229.         {
  230.             *buffer++ = *video++;
  231.             *buffer++ = *video++;
  232.         }
  233.     }
  234.     return(0);
  235. }
  236.  
  237.                                         /* put text from buffer into video */
  238. int puttext(int left, int top, int right, int bottom, char *buffer)
  239. {
  240.     int i,j;
  241.     char far *video;
  242.  
  243.     for(i = top - 1; i < bottom; i++)
  244.     {
  245.         FP_SEG(video) = FP_SEG(biosvid);
  246.         FP_OFF(video) = ((i * 80) + (left - 1) ) * 2;
  247.  
  248.         for(j = left - 1; j < right; j++)
  249.         {
  250.             *video++ = *buffer++;
  251.             *video++ = *buffer++;
  252.         }
  253.     }
  254.     return(0);
  255. }
  256.  
  257. int wherex(void)                        /* get current x coordinate        */
  258. {
  259.     union REGS regs;
  260.  
  261.     regs.h.ah = 0x03;
  262.     regs.h.bh = 0;
  263.  
  264.     int86(0x10, ®s, ®s);
  265.  
  266.     return(regs.h.dl + 1);
  267. }
  268.  
  269. int wherey(void)                        /* get current y coordinate        */
  270. {
  271.    union REGS regs;
  272.  
  273.     regs.h.ah = 0x03;
  274.     regs.h.bh = 0;
  275.  
  276.     int86(0x10, ®s, ®s);
  277.  
  278.     return(regs.h.dh + 1);
  279. }
  280. #define bioskey(c) _bios_keybrd((c))
  281.  
  282. #endif
  283.         /************************************************************
  284.         *  Video Support Routines                                   *
  285.         *********************************************************CR*/
  286.  
  287. void c_str(int row, char *str)          /* Print a string, centered        */
  288. {
  289.     unsigned wid;                       /* temporary width variable        */
  290.  
  291.     wid = (80 - strlen(str))/2;         /* calculate cursor position       */
  292.  
  293.     gotoxy(wid,row);                    /* go there                        */
  294.  
  295.     cputs(str);                         /* display the string              */
  296. }
  297.  
  298. void SaveCursor(void)                   /* save current cursor size and    */
  299. {                                       /*   position                      */
  300. #ifdef MSC5
  301.  
  302. #define MONO 7
  303.  
  304.     union REGS regs;
  305.  
  306.     regs.h.ah = 3;
  307.     regs.h.bh = 0;
  308.  
  309.     int86(0x10, ®s, ®s);
  310.  
  311.     oldpos = regs.x.dx;
  312.     oldcur = regs.x.cx;
  313.  
  314. #else
  315.  
  316.     _AH = 3;                            /* Get Cursor Position             */
  317.     _BH = 0;
  318.     _VideoInt();
  319.  
  320.     oldpos = _DX;                       /* Save return values              */
  321.     oldcur = _CX;
  322.  
  323. #endif
  324.                                         /* known bug on some monochrome    */
  325.                                         /*   adapters reports the wrong    */
  326.                                         /*   cursor shape when both color  */
  327.                                         /*   and monochrome systems are    */
  328.                                         /*   installed.                    */
  329.     if( (curmode == MONO) && (oldcur == 0x0607) )
  330.         oldcur = 0x0c0d;
  331.  
  332.     NoCursor();                         /* Make cursor hidden              */
  333. }
  334.  
  335. void RestoreCursor(void)                /* restore saved cursor position   */
  336. {                                       /*   and size                      */
  337. #ifdef MSC5
  338.  
  339.     union REGS regs;
  340.  
  341.     regs.h.ah = 2;
  342.     regs.h.bh = 0;
  343.     regs.x.dx = oldpos;
  344.  
  345.     int86(0x10, ®s, ®s);
  346.  
  347.     regs.h.ah = 1;
  348.     regs.h.bh = 0;
  349.     regs.x.cx = oldcur;
  350.  
  351.     int86(0x10, ®s, ®s);
  352.  
  353. #else
  354.  
  355.     _AH = 2;                            /* restore saved position          */
  356.     _BH = 0;
  357.     _DX = oldpos;
  358.     _VideoInt();
  359.  
  360.     _AH = 1;                            /* restore saved cursor type       */
  361.     _BH = 0;
  362.     _CX = oldcur;
  363.     _VideoInt();
  364.  
  365. #endif
  366. }
  367.  
  368. void NoCursor(void)                     /* turn off cursor                 */
  369. {
  370. #ifdef MSC5
  371.  
  372.     union REGS regs;
  373.  
  374.     regs.h.ah = 1;
  375.     regs.x.cx = 0xf0f0;
  376.  
  377.     int86(0x10, ®s, ®s);
  378.  
  379. #else
  380.  
  381.     _AH = 1;
  382.     _CX = 0xf0f0;
  383.     _VideoInt();
  384.  
  385. #endif
  386. }
  387.  
  388. void BigCursor(void)                    /* use block cursor                */
  389. {
  390. #ifdef MSC5
  391.  
  392.     union REGS regs;
  393.  
  394.     regs.h.ah = 1;
  395.     regs.x.cx = curtype;
  396.  
  397.     int86(0x10, ®s, ®s);
  398. #else
  399.  
  400.     _AH = 1;
  401.     _CX = curtype;
  402.     _VideoInt();
  403.  
  404. #endif
  405. }
  406.  
  407. void GetVideoMode(void)
  408. {
  409. #ifdef MSC5
  410.  
  411.     union REGS regs;
  412.  
  413.     regs.h.ah = 0x0f;
  414.  
  415.     int86(0x10, ®s, ®s);
  416.  
  417.     curmode = regs.h.al;
  418.     pagenum = regs.h.bh;
  419.  
  420. #else
  421.  
  422.     _AH = 0x0f;
  423.     _VideoInt();
  424.  
  425.     curmode = _AL;
  426.     pagenum = _BH;
  427.  
  428. #endif
  429. }
  430.  
  431. void fixrows(void)                      /* Re-initialize current video     */
  432. {                                       /*   information for new instance  */
  433.                                         /*   of video usage                */
  434. #ifdef TC
  435.     extern char _video[];               /* Undocumented Video Data region  */
  436.     extern void _crtinit(int newmode);  /* Internal Initialization Routine */
  437. #endif
  438.  
  439. #ifdef MSC5
  440. #define BW40 0
  441. #define C40  1
  442. #define BW80 2
  443. #define C80  3
  444.  
  445.     GetVideoMode();
  446.  
  447.     maxrows = (*((unsigned char far *)0x484) + 1);
  448.     if(maxrows < 25)
  449.         maxrows = 25;
  450.  
  451. #else
  452.     _crtinit(curmode);                  /* re-initialize video subsystem   */
  453.  
  454.     maxrows = _video[7];
  455. #endif
  456.  
  457.     switch(curmode)                     /* deal with text mode             */
  458.     {
  459.         case BW40:
  460.             textmode(BW80);             /* we need 80 columns              */
  461.         case BW80:
  462.         case MONO:
  463.             NormAtt = MONONORM;         /* use Monochrome Attributes       */
  464.             RevAtt = MONOREV;
  465.             break;
  466.         case C40:
  467.             textmode(C80);              /* we need 80 columns              */
  468.         case C80:
  469.             NormAtt = COLORNORM;        /* use Color attributes            */
  470.             RevAtt = COLORREV;
  471.             break;
  472.     }
  473.  
  474.  
  475.     if(curmode == MONO)                 /* If monochrome ....              */
  476.     {
  477. #ifdef MSC5
  478.         FP_SEG(biosvid) = 0xb000;
  479.         FP_OFF(biosvid) = 0;
  480. #else
  481.         biosvid = MK_FP(0xb000,0);      /* ... set pointer and cursor      */
  482. #endif
  483.         curtype = 0x000d;
  484.     }
  485.     else                                /* That means color ....           */
  486.     {
  487. #ifdef MSC5
  488.         FP_SEG(biosvid) = 0xb800;
  489.         FP_OFF(biosvid) = 0;
  490. #else
  491.         biosvid = MK_FP(0xb800,0);      /* ... so set pointer and cursor   */
  492.         curtype = 0x0007;
  493. #endif
  494.     }
  495.  
  496. }
  497.  
  498.  
  499. /*****************************< main          >******************************
  500. *                                                                           *
  501. *                         main routine of C program                         *
  502. *                         -------------------------                         *
  503. *                                                                           *
  504. *   Simple-minded main.  Calculates top of background stack region,         *
  505. *       sets the stack points for the TSR; tests to see if we are already   *
  506. *       resident; if so, displays ID number and exits.  If it is OK         *
  507. *       to install it goes resident with DoTsrInit(). Note that InitTsrDemo *
  508. *       is called by TsrCleanUp().                                          *
  509. *                                                                           *
  510. *   Parameters:                                                             *
  511. *       none                                                                *
  512. *                                                                           *
  513. *   Returns:                                                                *
  514. *       none                                                                *
  515. *                                                                           *
  516. *************************************************************************CR*/
  517.  
  518.  
  519. struct ExtraHot MyKeys[2] = {
  520.     { TSRHOT_X, TSRPOPALT, 1 },
  521.     { TSRHOT_Y, TSRPOPCTRL, 2 }
  522.     };
  523.  
  524. void main(void)
  525. {
  526.     char far *stackptr1,                /* Pointer to top of Popup Stack   */
  527.          far *stackptr2;                /* Pointer to top of Background    */
  528.                                         /*   stack area                    */
  529. #ifdef MSC5
  530.     extern unsigned _atopsp;            /* undocumented offset of top of   */
  531.                                         /*   MSC5 stack area               */
  532.     extern unsigned pascal STKHQQ;      /* undocumented offset of base of  */
  533.                                         /*   MSC5 stack area (plus fudge)  */
  534.     struct SREGS sregs;
  535. #else
  536.     extern unsigned __heapbase,         /* undocumented offset of base of  */
  537.                                         /*   TC 1.5 heap area              */
  538.                     _heaplen,           /* size of heap                    */
  539.                     _stklen;            /* size of stack                   */
  540. #endif
  541.  
  542. #ifdef MSC5
  543.  
  544.     segread(&sregs);
  545.  
  546.     FP_SEG(stackptr2) = sregs.ds;
  547.     FP_OFF(stackptr2) = _atopsp - ((_atopsp - STKHQQ) / 2);
  548.  
  549.     FP_SEG(stackptr1) = sregs.ds;
  550.     FP_OFF(stackptr1) = _atopsp;
  551.  
  552. #else
  553.     stackptr1 = MK_FP(_DS, __heapbase + _heaplen + (_stklen / 2) - 16);
  554.     stackptr2 = MK_FP(_DS, __heapbase + _heaplen + _stklen - 16);
  555. #endif
  556.  
  557.     TsSetStack(stackptr1, stackptr2);   /* Set Popup Stack to stackptr1    */
  558.                                         /*   background stack to stackptr2 */
  559.  
  560.  
  561.                                         /* Are we already here?            */
  562. #ifdef MSC5
  563.     if(TsCheckResident("TESSMSC ",&idnum) == 0xffff)
  564. #else
  565.     if(TsCheckResident("TESSDEMO",&idnum) == 0xffff)
  566. #endif
  567.     {
  568.                                         /* Yep!                            */
  569.         puts("The TesSeRact Demonstration TSR has already been loaded");
  570.  
  571.         if(idnum & 0xff00)              /* if released                     */
  572.         {
  573.             puts("  But it is currently waiting to be released from memory");
  574.             puts("  Restarting the TesSeRact Demonstration Program Now");
  575.             TsRestart(idnum & 0x00ff);
  576.         }
  577.         else
  578.         {
  579.             puts("  Use ALT-LeftShift-R to PopUp the TsrMain() routine");
  580.             printf("  Use ID Number %d to communicate through TesSeRact " \
  581.                 "Multiplex functions\n",idnum);
  582.         }
  583.         exit(1);
  584.     }
  585.  
  586.     if(TsCheckHotkey(TSRHOT_R) == 0xffff) /* is hotkey safe?                 */
  587.     {                                   /*  ... nope!                      */
  588.         puts("The TesSeRact Demonstration TSR cannot be loaded because");
  589.         puts("  another TSR currently resident on this system is using");
  590.         puts("  the same hotkey!");
  591.         exit(1);
  592.     }
  593. #ifdef NOTSR
  594.     TsrMain();                          /* Test to call TsrMain            */
  595.     bioskey(0);
  596. #else
  597.  
  598.     if( TsDoInit(
  599.         TSRHOT_R,
  600.         TSRPOPALT + TSRPOPLSHIFT,
  601.         TSRUSEPOPUP + TSRUSEBACK + TSRUSETIMER + TSRUSEUSER + NOPOPGRAPH,
  602.         SizeOfCode(ALLSTACK)) )
  603.         puts("Bad DoInit\n");
  604. #endif
  605.  
  606. }
  607.  
  608. /*****************************< SizeOfCode    >******************************
  609. *                                                                           *
  610. *                 Determine size of program to keep resident                *
  611. *                 ------------------------------------------                *
  612. *                                                                           *
  613. *   This function is an example of a function that can be used to determine *
  614. *       the size of the TSR that is to remain resident.  There are three    *
  615. *       options to this function -- NOHEAP, ALLHEAP, and ALLSTACK.  ALLHEAP *
  616. *       and ALLSTACK are identical with MSC 5.0 -- the stack is below       *
  617. *       the heap, and the stack will be part of the NOHEAP version as well. *
  618. *       In Turbo C 1.5, with the stack ABOVE the heap in tiny and small     *
  619. *       models, we can keep part of the heap, but drop off the stack.       *
  620. *       Example code is shown for both MSC 5 and TC; other compilers and    *
  621. *       langauges can determine the appropriate info as well.               *
  622. *                                                                           *
  623. *   Parameters:                                                             *
  624. *       type             NOHEAP, ALLHEAP or ALLSTACK parameters define above*
  625. *                                                                           *
  626. *   Returns:                                                                *
  627. *       Number of 16-byte paragraphs of memory to keep when going resident. *
  628. *                                                                           *
  629. *************************************************************************CR*/
  630.  
  631. unsigned SizeOfCode(unsigned type)
  632. {
  633. #ifdef  MSC5
  634.     unsigned int far *PSP;              /* far pointer to PSP              */
  635.     extern unsigned _psp,               /* segment of PSP                  */
  636.                     _atopsp;            /* undocumented offset of top of   */
  637.                                         /*   MSC 5.0 stack                 */
  638. #endif  /* End of MSC5 */
  639. #ifdef  TC
  640.     extern unsigned _psp,               /* segment address of PSP          */
  641.                     __heapbase,         /* undocumented offset of base of  */
  642.                                         /*   TC 1.5 heap area              */
  643.                     _heaplen,           /* size of heap                    */
  644.                     _stklen;            /* size of stack                   */
  645. #endif  /* End of TC */
  646.  
  647.     unsigned used;                      /* variable to save paragraphs     */
  648.     struct SREGS sregs;                 /* segment register structure      */
  649.  
  650.     segread(&sregs);                    /* read the segment regs           */
  651.  
  652.     switch(type)
  653.     {
  654.         case ALLSTACK:
  655. #ifdef TC
  656.             used = (((__heapbase + 16 + _heaplen + _stklen) >> 4) + sregs.ds) - _psp;
  657.             break;
  658. #endif
  659.         case ALLHEAP:
  660. #ifdef MSC5
  661.             FP_SEG(PSP) = _psp;         /* segment address of psp          */
  662.             FP_OFF(PSP) = 0;            /* offset of the psp is zero       */
  663.             used = *(PSP+1) - _psp;     /* number of paras used by program */
  664. #endif
  665. #ifdef TC
  666.             used = (((__heapbase + 16 + _heaplen) >> 4) + sregs.ds) - _psp;
  667. #endif
  668.             break;
  669.         case NOHEAP:
  670. #ifdef  MSC5
  671.             used = (((_atopsp + 16) >> 4) + sregs.ds) - _psp;
  672. #endif  /* End of MSC5 */
  673. #ifdef  TC
  674.             used = (((__heapbase + 16) >> 4) + sregs.ds) - _psp;
  675. #endif  /* End of TC */
  676.             break;
  677.     }
  678.  
  679.     return(used);                       /* return number of paragraphs     */
  680. }
  681.  
  682. /*****************************< do_cpyrt      >******************************
  683. *                                                                           *
  684. *                       Display Copyright Information                       *
  685. *                       -----------------------------                       *
  686. *                                                                           *
  687. *   Function to display formatted copyright information on the screen.      *
  688. *                                                                           *
  689. *   Parameters:                                                             *
  690. *       none                                                                *
  691. *                                                                           *
  692. *   Returns:                                                                *
  693. *       none                                                                *
  694. *                                                                           *
  695. *************************************************************************CR*/
  696.  
  697. void do_cpyrt(void)
  698. {
  699.     textattr(RevAtt);
  700.     c_str(2,"The TesSeRact Demonstration Program");
  701.     textattr(NormAtt);
  702.  
  703.     gotoxy(12,4);
  704.     cputs("Copyright 1986, 1987, 1988, TesSeRact Development Team");
  705.  
  706.     gotoxy(12,5);
  707.     cputs("All Rights Reserved");
  708. }
  709.  
  710. /*****************************< DisplayTime   >******************************
  711. *                                                                           *
  712. *                     'Poke' current time into video RAM                    *
  713. *                     ----------------------------------                    *
  714. *                                                                           *
  715. *   Adjusts minutes and seconds, and then pokes the holding buffer into     *
  716. *       the first 8 character bytes of the Video RAM segment.  Note that    *
  717. *       the 'hours' will be adjusted by the AdjustTime function.            *
  718. *                                                                           *
  719. *   Parameters:                                                             *
  720. *       none                                                                *
  721. *                                                                           *
  722. *   Returns:                                                                *
  723. *       none                                                                *
  724. *                                                                           *
  725. *************************************************************************CR***/
  726.  
  727. void DisplayTime(void)
  728. {
  729.     int i;
  730.  
  731.     buffer[0] = (hours / 10) + 0x30;
  732.     buffer[1] = (hours % 10) + 0x30;
  733.  
  734.     buffer[3] = (mins / 10) + 0x30;
  735.     buffer[4] = (mins % 10) + 0x30;
  736.  
  737.     buffer[6] = (secs / 10) + 0x30;
  738.     buffer[7] = (secs % 10) + 0x30;
  739.  
  740.     for(i=0;i<8;i++)
  741.         biosvid[i*2] = buffer[i];
  742. }
  743.  
  744. /*****************************< AdjustTime    >******************************
  745. *                                                                           *
  746. *                     Call DOS to get the current time                      *
  747. *                     --------------------------------                      *
  748. *                                                                           *
  749. *   Calls DOS to get the current time, save it to clobal values, and then   *
  750. *       calls the C runtime sprintf() function to format it into the buffer *
  751. *                                                                           *
  752. *   Parameters:                                                             *
  753. *       none                                                                *
  754. *                                                                           *
  755. *   Returns:                                                                *
  756. *       none                                                                *
  757. *                                                                           *
  758. *************************************************************************CR***/
  759.  
  760. void AdjustTime(void)
  761. {
  762. #ifdef MSC5
  763.     struct dostime_t timep;
  764.  
  765.     _dos_gettime(&timep);
  766.  
  767.     hours = timep.hour;
  768.     mins = timep.minute;
  769.     secs = timep.second;
  770. #else
  771.     struct time timep;
  772.  
  773.     gettime(&timep);
  774.  
  775.     hours = timep.ti_hour;
  776.     mins = timep.ti_min;
  777.     secs = timep.ti_sec;
  778. #endif
  779.  
  780.     sprintf(buffer,"%02d:%02d:%02d",hours,mins,secs);
  781. }
  782.  
  783. /*****************************< InitTsrDemo   >******************************
  784. *                                                                           *
  785. *                      Initialize variables and video                       *
  786. *                      ------------------------------                       *
  787. *                                                                           *
  788. *   This function just initializes everything, displays a sign-on message,  *
  789. *       and gets the clock info for the first time.                         *
  790. *                                                                           *
  791. *   Parameters:                                                             *
  792. *       none                                                                *
  793. *                                                                           *
  794. *   Returns:                                                                *
  795. *       none                                                                *
  796. *                                                                           *
  797. *************************************************************************CR***/
  798.  
  799. void InitTsrDemo(void)
  800. {
  801.     GetVideoMode();                     /* save current mode for later     */
  802.  
  803.     fixrows();
  804.  
  805.     clrscr();
  806.  
  807.     window(1,1,80,8);
  808.     textattr(NormAtt);
  809.     clrscr();
  810.  
  811.     do_cpyrt();
  812.  
  813.     c_str(7,"Press Alt-LeftShift-R to activate the TesSeRact " \
  814.         "Demonstration Program\n ");
  815.  
  816.     AdjustTime();
  817.     DisplayTime();
  818.  
  819.     if(fp == NULL)
  820.     {
  821.         fp = fopen("Tessdemo.dat","a+b");
  822.         fprintf(fp,"TesSeRact Demonstration Program loaded at %s\n\r",buffer);
  823.         FLUSHIT(fp);
  824.     }
  825. }
  826.  
  827.         /************************************************************
  828.         *   TSR Procedures                                          *
  829.         *********************************************************CR*/
  830.  
  831. char *StuffBuf = "\x72\x13\x69\x17\x6e\x31\x67\x22";
  832. unsigned StuffLen = 4;
  833.  
  834. void far pascal TsrMain(void)
  835. {
  836.     unsigned oldstat,curdisk,ret;
  837.     long bypercl, frees, total;
  838.     unsigned char SaveVideoPageNum = 0;
  839.     struct TsrParms far *ParmsPtr;
  840. #ifdef MSC5
  841.     struct diskfree_t diskfree;
  842.     union REGS regs;
  843. #else
  844.     struct dfree diskfree;
  845. #endif
  846.  
  847.     GetVideoMode();                     /* save current mode for later     */
  848.  
  849.     SaveCursor();
  850.  
  851.     if(pagenum)
  852.     {
  853.         SaveVideoPageNum = pagenum;
  854.  
  855. #ifdef MSC5
  856.         regs.x.ax = 0x0500;
  857.         int86(0x10, ®s, ®s);
  858. #else
  859.         _AX = 0x0500;
  860.         _VideoInt();
  861. #endif
  862.     }
  863.  
  864.     fixrows();
  865.  
  866.     window(1,1,80,maxrows);
  867.     gettext(1,1,80,maxrows,savescreen);
  868.  
  869.     textattr(NormAtt);
  870.     clrscr();
  871.  
  872.     do_cpyrt();
  873.  
  874.     ParmsPtr = TsGetParms(idnum);
  875.  
  876.     oldstat = TsGetStat(idnum);
  877.  
  878.     gotoxy(5,7);
  879.     cprintf("This TSR popped up with HotKey #%d, and is using the "
  880.                 "following procedures:", ParmsPtr->HotKeyFlag);
  881.  
  882.     if(oldstat & TSRUSEPOPUP)
  883.     {
  884.         gotoxy(10,wherey()+1);
  885.         cputs("User-Defined PopUp Procedure");
  886.     }
  887.     if(oldstat & TSRUSEBACK)
  888.     {
  889.         gotoxy(10,wherey()+1);
  890.         cputs("User-Defined Background Procedure");
  891.     }
  892.     if(oldstat & TSRUSETIMER)
  893.     {
  894.         gotoxy(10,wherey()+1);
  895.         cputs("User-Defined Timer Procedure");
  896.     }
  897.     if(oldstat & TSRUSEUSER)
  898.     {
  899.         gotoxy(10,wherey()+1);
  900.         cputs("User-Defined User Communication Procedure");
  901.     }
  902.  
  903. #ifdef MSC5
  904.  
  905.     _dos_getdrive(&curdisk);
  906.  
  907.     _dos_getdiskfree(curdisk, &diskfree);
  908.  
  909.     bypercl = (long)(diskfree.bytes_per_sector * diskfree.sectors_per_cluster);
  910.     frees = bypercl * diskfree.avail_clusters;
  911.     total = bypercl * diskfree.total_clusters;
  912.  
  913. #else
  914.  
  915.     curdisk = getdisk() + 1;
  916.  
  917.     getdfree(curdisk, &diskfree);
  918.  
  919.     bypercl = (long)(diskfree.df_bsec * diskfree.df_sclus);
  920.     frees = bypercl * diskfree.df_avail;
  921.     total = bypercl * diskfree.df_total;
  922.  
  923. #endif
  924.  
  925.     gotoxy(5,19);
  926.     cprintf("Current disk is %c:, with %ld bytes available, %ld total bytes",
  927.         curdisk + 0x40, frees, total);
  928.  
  929.     fprintf(fp,"TesSeRact Demonstration Program popped up at %s\n\r",buffer);
  930.     FLUSHIT(fp);
  931.  
  932.     gotoxy(5,21);
  933.     cprintf("This TSR is called %-8.8Fs, and has a PSP at segment %04x",
  934.         ParmsPtr->IdCode, ParmsPtr->TsrPSP);
  935.  
  936.     gotoxy(25,22);
  937.     cprintf("Supported functions are %lx", ParmsPtr->FuncFlags);
  938.     
  939.     c_str(24,"Press 'R' to remove TSR from RAM; 'K' to stuff keyboard; any other key to exit");
  940.  
  941.     ret = bioskey(0) & 0xff;
  942.     if(toupper(ret) == 'R')
  943.         TsRelease(idnum);
  944.     else
  945.         if(toupper(ret) == 'K')
  946.             TsStuffKeyboard(idnum, StuffBuf, StuffLen, STUFF_FAST);
  947.     puttext(1,1,80,maxrows,savescreen);
  948.  
  949.     if(SaveVideoPageNum)
  950.     {
  951. #ifdef MSC5
  952.         regs.h.ah = 0x05;
  953.         regs.h.al = SaveVideoPageNum;
  954.         int86(0x10, ®s, ®s);
  955. #else
  956.         _AH = 0x05;                     /* Routine to restore correct      */
  957.         _AL = SaveVideoPageNum;         /*   video page; courtesy of Bruce */
  958.         _VideoInt();                    /*   Kitchin                       */
  959. #endif
  960.     }
  961.  
  962.     RestoreCursor();
  963. }
  964.  
  965. unsigned far pascal TsrBackCheck(void)
  966. {
  967.     return(BackFlag);
  968. }
  969.  
  970. void far pascal TsrBackProc(void)
  971. {
  972.     AdjustTime();
  973.     fprintf(fp,"TesSeRact Demonstration Program adjusted time at %s\n\r",buffer);
  974.     FLUSHIT(fp);
  975.     BackFlag = 0;
  976. }
  977.  
  978. void far pascal TsrTimerProc(void)
  979. {
  980.     if(++ticks > 18)
  981.     {
  982.         ticks = 0;                      /* always clear ticks if > 18      */
  983.         secs++;
  984.         switch(secs)
  985.         {
  986.             case 60:
  987.                 secs = 0;               /* reset ticks for display & count */
  988.                 if(++mins > 59)         /* inc mins                        */
  989.                 {
  990.                     mins = 0;           /* flip the minutes                */
  991.                     if(++hours > 23)    /* update the hours                */
  992.                         hours = 0;
  993.                 }
  994.                 BackFlag = 1;
  995.                 break;
  996.             case 20:
  997.             case 40:
  998.                 secs++;                 /* fudge for approx ticks          */
  999.                 break;
  1000.         }
  1001.         DisplayTime();                  /* always display time!            */
  1002.     }
  1003. }
  1004.  
  1005. void far pascal TsrUserProc(void far *UserPtr)
  1006. {
  1007.     printf("This is the user procedure:  Passed ptr = %Fs\n",UserPtr);
  1008. }
  1009.  
  1010. void far pascal TsrCleanUp(unsigned InitOrShutdown)
  1011. {
  1012. #ifdef TC
  1013.     extern void _restorezero(void);
  1014. #else
  1015.     extern void _ctermsub(void);
  1016. #endif  /* End of  */
  1017.  
  1018.     if(InitOrShutdown)      /* if we're shutting down          */
  1019.     {
  1020.         if(fp != NULL)
  1021.         {
  1022.             fprintf(fp,"TesSeRact Demonstration Program "
  1023.                 "released at %s\n\r",buffer);
  1024.             fclose(fp);
  1025.         }
  1026.     /*
  1027.      * Please note that it is *absolutely* vital to call _restorezero()
  1028.      *   or _ctermsub() at this point -- otherwise, the INT 0 vector
  1029.      *   is not restored, and a divide-by-zero exception will cause
  1030.      *   a crash, rather than a clean exit.  Note that this routine
  1031.      *   is compiler-dependent .... CR
  1032.      */
  1033.  
  1034. #ifdef TC
  1035.         _restorezero();
  1036. #else
  1037.         _ctermsub();
  1038. #endif
  1039.  
  1040.     }
  1041.     else
  1042.     {
  1043.         TsSetExtraHot(idnum, 2, MyKeys);
  1044.         InitTsrDemo();
  1045.     }
  1046. }
  1047.  
  1048.